package my.dict.v01;

import java.io.File;
import java.io.IOException;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;

public class Main extends TabActivity implements OnInitListener {
    /** Called when the activity is first created. */
	
	static String logcat = "DictionaryLC";
	
    private static final int REQ_TTS_STATUS_CHECK = 0; 
    private static final String TAG = "TTS Demo"; 
    static TextToSpeech mTts; 
	
	
//    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        intent = new Intent().setClass(this, Wordlist.class);
        spec = tabHost.newTabSpec("wordlist").setIndicator("List")
                      .setContent(intent);
        tabHost.addTab(spec);
        
                
        intent = new Intent().setClass(this, History.class);
        spec = tabHost.newTabSpec("history").setIndicator("History",
                          res.getDrawable(R.drawable.tab_history))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Web.class);
        spec = tabHost.newTabSpec("web").setIndicator("Web",
                          res.getDrawable(R.drawable.tab_web))
                      .setContent(intent);
        tabHost.addTab(spec);
               
        intent = new Intent().setClass(this, Note.class);
        spec = tabHost.newTabSpec("note").setIndicator("Note",
                          res.getDrawable(R.drawable.tab_note))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, Setting.class);
        spec = tabHost.newTabSpec("setting").setIndicator("Setting",
                          res.getDrawable(R.drawable.tab_setting))
                      .setContent(intent);
        tabHost.addTab(spec);       
       
        tabHost.setCurrentTab(0);  
        
        
        Log.d(logcat, "Number of tabs: " + 
        		tabHost.getTabWidget().getChildCount());

        
        int childCount = tabHost.getTabWidget().getChildCount();
        for (int i = 0; i < childCount; i++ ) {
        	tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 35;
        }
        
        try {
	        Log.d(logcat, "Current directory: " +
	        		new File(".").getCanonicalPath());
        } catch (IOException ioe) {
        	ioe.printStackTrace();
        }
        
        // Check to be sure that TTS exists and is okay to use 
        Intent checkIntent = new Intent(); 
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); 
        
    }
    

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == REQ_TTS_STATUS_CHECK) { 
            switch (resultCode) { 
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS: 
                // TTS is up and running 
                mTts = new TextToSpeech(this, this); 
                Log.v(TAG, "Pico is installed okay"); 
                break; 
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA: 
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: 
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME: 
                // missing data, install it 
                Log.v(TAG, "Need language stuff: " + resultCode); 
                Intent installIntent = new Intent(); 
                installIntent.setAction( 
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
                startActivity(installIntent); 
                break; 
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL: 
            default: 
                Log.e(TAG, "Got a failure. TTS apparently not available"); 
            } 
        } 
        else { 
            // Got something else 
        } 
    } 
 
    @Override 
    public void onInit(int status) {
    	
    	Log.v(TAG, "onInit has been called"); 
    	
        // Now that the TTS engine is ready, we enable the button 
        if( status == TextToSpeech.SUCCESS) { 
            //speakBtn.setEnabled(true); 
            
            Log.v(TAG, "TTS engine is ready, we enable the button"); 
        } 
    } 
 
    @Override 
    public void onPause() 
    { 
        super.onPause(); 
        // if we're losing focus, stop talking 
        if( mTts != null) 
            mTts.stop(); 
    } 
    @Override 
    public void onDestroy() 
    { 
        super.onDestroy(); 
        mTts.shutdown(); 
    } 
} 